﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AplicacaoReserva.ClientesForms
{
    public partial class ListarClientesForm : Form
    {

        public int ID;

        public ListarClientesForm()
        {
            InitializeComponent();
        }

        private void ListarClientesForm_Load(object sender, EventArgs e)
        {
            try
            {
                ClienteServiceReference.WCFClienteClient clientes = new ClienteServiceReference.WCFClienteClient();

                dgv_ListaClientes.DataSource = clientes.list();

                clientes.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Occoreu um erro:\n" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEditarClientes_Click(object sender, EventArgs e)
        {
            EditarClienteForm editarCliente = new EditarClienteForm();

            editarCliente.MdiParent = MdiParent;

            editarCliente.clienteID = ID;

            editarCliente.Show();
        }

        private void btnConsultaReservas_Click(object sender, EventArgs e)
        {
            ReservasForms.ListarReservasForm listarReservasCliente = new ReservasForms.ListarReservasForm();

            listarReservasCliente.ClienteID = ID;

            listarReservasCliente.MdiParent = MdiParent;

            listarReservasCliente.Show();
        }

        private void btnRemoverClientes_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Tens a certeza?", "Remover Cliente", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) {
                try
                {
                    ClienteServiceReference.WCFClienteClient clientes = new ClienteServiceReference.WCFClienteClient();

                    clientes.delete(ID);

                    MessageBox.Show("Removido com sucesso!", "Remover Cliente", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.Close();
                }
                catch (Exception ex) {
                    MessageBox.Show("Occoreu um erro:\n" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void dgv_ListaClientes_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ID = int.Parse(dgv_ListaClientes.Rows[e.RowIndex].Cells[0].Value.ToString());

            btnEditarClientes.Enabled = true;
            btnConsultaReservas.Enabled = true;
            btnRemoverClientes.Enabled = true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
